var Upload = {
	init: function(uploadHttp, cb, multi, isFile){
		Upload.files = new Array();
		Upload.res = new Array();
		Upload.currentRotate = new Array();
		Upload.fileSuccCount = 0;
		Upload.fileErrorCount = 0;
		Upload.uploadHttp = uploadHttp;
		Upload.cb = cb;
		Upload.multi = multi;
		Upload.isFile = isFile;

		Upload.ExifImageWidth = new Array();
		Upload.ExifImageLength = new Array();
		// if (uexWidgetOne.platformName == "android")
		// 	Upload.multi = multi;
		// else
		// 	Upload.multi = undefined;
		Upload.index = 0;
		if (isFile) {
			Upload.fileopen();
		}
		else {
			uexWindow.cbActionSheet = function(opId, dataType, data) {
				if (uexWidgetOne.platformName == "Simulator" && (data == 0 || data == 1)) {
					Upload.cb({code:0, result:{url:"img/director.jpg"}});
					return;
				}
				if (data == 0)
					Upload.paiopen();
				else if (data == 1)
					Upload.imgopen();
			}
			uexWindow.actionSheet("", "取消", new Array("拍照", "从本地相册选择"));
		}
	},
	paiopen: function(){
		Upload.type = "paiopen";
		uexCamera.cbOpen = Upload.browserAfter;
		uexCamera.open();
	},
	imgopen: function() {
		Upload.type = "imgopen";
		uexImageBrowser.cbPick = Upload.browserAfter;
		if (Upload.multi)
			uexImageBrowser.pickMulti(Upload.multi);
		else
			uexImageBrowser.pick();
	},
	fileopen: function() {
		Upload.type = "fileopen";
		if (Upload.multi) {
			uexFileMgr.cbMultiExplorer = Upload.browserAfter;
			uexFileMgr.multiExplorer("file:///sdcard/");
		}
		else {
			uexFileMgr.cbExplorer = Upload.browserAfter;
			uexFileMgr.explorer("file:///sdcard/");
		}
	},
	upload: function() {
		var randOpId = Math.floor(Math.random() * (1000 + 1));
		var statusAfterFun = function(opCode, isSucc, res){
			// alert(Upload.index+"---->"+isSucc);
			// alert("opCode-->"+opCode);
			uexUploaderMgr.closeUploader(opCode);
			if (isSucc) {
				Upload.res.push(eval('(' + res + ')'));
				++Upload.fileSuccCount;
			} else {
				++Upload.fileErrorCount;
			}
			++Upload.index;
			if (Upload.files.length == Upload.index) {
				var errStr = "";
				if (Upload.fileErrorCount > 0) {
					if (Upload.isFile)
						errStr = "，"+ Upload.fileErrorCount +"个文件上传失败";
					else
						errStr = "，"+ Upload.fileErrorCount +"张照片上传失败";
				}
				uexWindow.closeToast();
				if (Upload.isFile)
					uexWindow.toast("0", "5", Upload.fileSuccCount +"个文件上传成功"+errStr, 3000);
				else
					uexWindow.toast("0", "5", Upload.fileSuccCount +"张照片上传成功"+errStr, 3000);
				Upload.cb(Upload.res);
			} else
				Upload.upload();
		}
		uexUploaderMgr.onStatus = function(opCode, fileSize, percent, response, status) {
			switch (status) {
				case 0:
					percent = parseInt(100/Upload.files.length)*Upload.index+parseInt(parseInt(100/Upload.files.length)/100*percent);
					uexWindow.toast("0", "5", percent + "%", 0);
					break;
				case 1:
					statusAfterFun(opCode, true, response);
					break;
				case 2:
					//alert("fileSize-->"+fileSize);
					//alert("percent-->"+percent);
					//alert("response-->"+response);
					//alert("status-->"+status);
					statusAfterFun(opCode, false, response);
					break;
				default:
					uexUploaderMgr.closeUploader(opCode);
					break; 
			}
		}
		uexUploaderMgr.cbCreateUploader = function(opCode, dataType, data) {
			if (data == 0) {
				var ExifImageWidth = Upload.ExifImageWidth[Upload.index];
				var ExifImageLength = Upload.ExifImageLength[Upload.index];
				var inWidthLimit = 9999;
				// if (ExifImageWidth > 1024 && ExifImageLength > 1024)
					inWidthLimit = 1024;

				var inCompress = 3;
				if (Upload.uploadHttp.indexOf("/users/updatephoto") >= 0)
					inWidthLimit = 200;
				uexUploaderMgr.uploadFile(opCode, Upload.files[Upload.index], "file", inCompress, inWidthLimit);
			} else {
				uexWindow.closeToast();
				alert("创建上传对象失败");
			}
		}
		 uexWindow.toast('1', '5', '正在上传...', '');
		 //alert("randOpId-->"+randOpId);
		 //alert("Upload.uploadHttp-->"+Upload.uploadHttp);
		Upload.uploadHttp += (Upload.uploadHttp.indexOf("?") >= 0)?"&":"?";
		Upload.uploadHttp += "rotate=" + (Upload.currentRotate[Upload.index] / 90 % 4);
		uexUploaderMgr.createUploader(randOpId, Upload.uploadHttp);
	},
	browserAfter:function(opCode, dataType, data) {
		if (Upload.type == "fileopen") {  //附件相关的上传
			if (Upload.multi) {
				var json = eval("("+data+")");
	            for(var key in json){          
	                Upload.files.push(json[key]);
	            }
			} else
				Upload.files.push(data);
		} else {  //图片相关的上传
			if (Upload.multi){
				data = data.replace("[", "");
				data = data.replace("]", "");
				if (data.indexOf(", ") >= 0)
					Upload.files = data.split(", ");
				else
					Upload.files = data.split(",");
			}
			else {
				Upload.files = new Array(data);
			}

			for (var i=0; i<Upload.files.length; ++i) {
				$("body").append('<img id="UploadRotate" src="'+ Upload.files[i] +'" exif="true" class="hide" />');
				var orientation = $("#UploadRotate").exif("Orientation");
				$("#UploadRotate").remove();
				// alert("orientation---->>>>"+orientation);
				Upload.ExifImageWidth[i] = $("#UploadRotate").exif("ExifImageWidth");
				Upload.ExifImageLength[i] = $("#UploadRotate").exif("ExifImageLength");
				switch(orientation)
				{
					case 1:
						Upload.currentRotate[i] = 0;
						break;
					case 6:
						Upload.currentRotate[i] = 90;
						break;
					case 3:
						Upload.currentRotate[i] = 180;
						break;
					case 8:
						Upload.currentRotate[i] = 270;
						break;
					default:
				}
			}
		}
		// for (var i=0; i<Upload.files.length; ++i) {
		// 	alert(Upload.files[i]);
		// }
		// return;
		
		// alert("currentRotate__count---->>>>"+Upload.currentRotate.length);
		Upload.upload();
	}
}